package com.fancon.book.soixich;


import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class ChapterDetail extends Activity{
	private WebView mWebView;
	 /** Button Back */
    private int chapter;
    public final int maxChapter = 7;
    static final String[] CHAPTERS = new String[] { "Chương I    : Mở đầu",
		"Chương II   : Cuối tuần anh đưa tôi...",
		"Chương III  : Chiều nay ngồi buồn tôi nhớ đến...",
		"Chương IV   : Tôi tò mò mở cuốn băng ra xem...",
		"Chương V    : Vào đêm đó, vẫn như thường lệ...",
		"Chương VI   : Có tiếng kêu nho nhỏ...",
		"Chương VII  : Sau ngày mê hồn ấy, tôi như...",
		"Chương VIII : Đầu tôi lúc này xáo trộn..."
		 };
    
    
 // Menu option
	private int previousMenu = Menu.FIRST;  
    private int bookmarkMenu = Menu.FIRST + 1;
    private int nextMenu = Menu.FIRST + 2;
    private int groupId = 1; 
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
      menu.add(groupId, previousMenu , previousMenu, getResources().getString(R.string.menu_previous)).setIcon(R.drawable.back);  
      menu.add(groupId, bookmarkMenu , bookmarkMenu, getResources().getString(R.string.menu_bookmark)).setIcon(R.drawable.bookmark);
      menu.add(groupId, nextMenu , nextMenu, getResources().getString(R.string.menu_next)).setIcon(R.drawable.next);
      return true; 
    }  

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case Menu.FIRST:
			if (chapter > 0) {
				//Go to previous chapter
				chapter--;
				openChapter(chapter);
				setTitle(CHAPTERS[chapter]);
				
			} else {
				//This is the first chapter 
				showDialog(getString(R.string.cannot_back));
			}
			break;
		case Menu.FIRST + 1:
			//Write chapter into Sharepreference...
			PreferenceUtil.setChapter(getApplicationContext(), PreferenceUtil.PREFS_KEY, String.valueOf(chapter));
			Toast.makeText(getApplicationContext(), getString(R.string.bookmark_success), Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST +2:
			if (chapter < maxChapter) {
				//Go to next chapter
				chapter++;
				openChapter(chapter);
				setTitle(CHAPTERS[chapter]);
				
			} else {
				//This is the last chapter 
				showDialog(getString(R.string.cannot_next));
			}
			break;
		

		}
		return true;
	}
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapter_detail);
		Intent intent = getIntent();
			
		String title = intent.getStringExtra("title");
		chapter = intent.getIntExtra("chapter", -1);
		Log.d("chap", chapter + "");
		if (chapter > -1) {
			setTitle(title);
			openChapter(chapter);
		}
		AdView adview = (AdView) findViewById(R.id.adView);
		AdRequest re = new AdRequest();
		// re.setTesting(true);
		re.setGender(AdRequest.Gender.FEMALE);
		adview.loadAd(re);
	}
	
	private class DetailWebViewClient extends WebViewClient {
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        view.loadUrl(url);
	        return true;
	    }
	}
	
	/*
	 * show dialog when have a error
	 */
	public void showDialog(String msg) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg).setCancelable(true)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	/**
	 * open selected chapter
	 * 
	 * @param chapter
	 */
	private void openChapter(int chapter) {
	    mWebView = (WebView) findViewById(R.id.chapter_detail);
	    mWebView.getSettings().setJavaScriptEnabled(true);
	    mWebView.loadUrl("file:///android_asset/chapter" + chapter +".html");
	    mWebView.setWebViewClient(new DetailWebViewClient());
	}

   @Override
   public void onStop(){
   	super.onStop();
   }
   @Override
   public void onResume(){
   	super.onResume();
   }
}
